package com.cjlgb.design.upms.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cjlgb.design.common.mybatis.service.BaseService;
import com.cjlgb.design.upms.api.entity.SysUserRole;

import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @author WFT
 * @date 2019/8/13
 * description: 后台账号与角色关联的业务接口
 */
public interface SysUserRoleService extends BaseService<SysUserRole> {

    /**
     * 根据账号Id获取角色Id列表
     * @param accountId 账号Id
     * @return 角色Id列表
     */
    default Collection<Long> selectRoleIds(long accountId){
        return this.list(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getAccountId,accountId))
                .stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
    }

    /**
     * 根据角色Id分页查询账号Id
     * @param page 分页条件
     * @param roleId 角色Id
     * @return Collection<Long> 账号Id列表
     */
    default IPage<SysUserRole> pagingQuery(IPage<SysUserRole> page, long roleId){
        return this.page(page,Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getRoleId,roleId));
    }

    /**
     * 根据角色Id列表删除与菜单的关联
     * @param roleIds 角色Id列表
     */
    default void deleteByRoleIds(Collection<Long> roleIds){
        this.remove(Wrappers.<SysUserRole>lambdaQuery().in(SysUserRole::getRoleId,roleIds));
    }

    /**
     * 根据账号Id删除与角色的关联
     * @param accountId 账号Id
     */
    default void deleteByAccountId(long accountId){
        this.remove(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getAccountId,accountId));
    }

    /**
     * 根据账号Id列表删除与角色的关联
     * @param accountIds 账号Id列表
     */
    default void deleteByAccountIds(Collection<Long> accountIds){
        this.remove(Wrappers.<SysUserRole>lambdaQuery().in(SysUserRole::getAccountId,accountIds));
    }
}
